home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #18 (Mar 87) / Printer MPW sources / StringFormat.inc.p < prev    next >
Text File  |  1986-12-01  |  2KB  |  77 lines

  1. (*     @(#) StringFormat.inc.p 11/28/86
  2.  
  3.     WHAT:    Implementation of UNIT StringWrite
  4.     WHO:    Joel West, Western Software Technology
  5.     WHEN:    November 1986
  6.     HOW:    Formatted output to Pascal strings.  Names match
  7.         Modula-2 InOut module.  Developed to replace —
  8.         albeit awkwardly — use of C sprintf.
  9.     WHY:    Included from StringFormat.p (INTERFACE) for IMPLEMENTATION
  10. *)
  11.  
  12. { As with all the Pascal equivalents, output the specified field width
  13.   or the minimum necessary number of digits, whichever is greater.
  14.   Does not check for the output string > 255 characters
  15. }
  16.  
  17.    {*-----------------------------------*
  18.     | SWrite -- format a character    |
  19.     *-----------------------------------*}
  20.     PROCEDURE SWrite(VAR s: Str255; c: CHAR);
  21.     VAR
  22.             i : INTEGER;
  23.     BEGIN
  24.     i := ORD(s[0])+1;
  25.     s[0] := CHR(i);
  26.     s[i] := c;
  27.     END; (* SWrite *)
  28.  
  29.    {*-------------------------------------------*
  30.     | SWriteHex -- format a number in hex    |
  31.     *-------------------------------------------*}
  32.     PROCEDURE SWriteHex(VAR s: Str255; n: LongInt; w: INTEGER);
  33.     VAR
  34.         d: INTEGER;
  35.         s2: Str255;
  36.     BEGIN
  37.         s2[0] := CHR(w);
  38.     WHILE w > 0 DO
  39.     BEGIN
  40.             d := BAND(n,$F);
  41.         n := BSR(n,4);
  42.         IF d < 10 THEN
  43.             s2[w] := CHR(ORD('0') + d)
  44.         ELSE
  45.             s2[w] :=  CHR(ORD('A')-10+d);
  46.         w := w-1;
  47.     END;
  48.         SWriteString(s,s2);
  49.     END; (* SWriteHex *)
  50.  
  51.    {*-------------------------------------------*
  52.     | SWriteInt -- format a number in decimal    |
  53.     *-------------------------------------------*}
  54.     PROCEDURE SWriteInt(VAR s: Str255; n: LongInt; w: INTEGER);
  55.         VAR
  56.         i: INTEGER;
  57.         s2: Str255;
  58.     BEGIN
  59.         NumToString(n, s2);
  60.     i := w - Length(s2);
  61.     WHILE i > 0 DO
  62.     BEGIN
  63.         SWrite(s, ' ');    (* Leading spaces *)
  64.         i := i-1;
  65.     END;
  66.         SWriteString(s,s2);
  67.     END; (* SWriteInt *)
  68.  
  69.    {*-------------------------------------------*
  70.     | SWriteString -- format a character string    |
  71.     *-------------------------------------------*}
  72.     PROCEDURE SWriteString(VAR s: Str255; s2: Str255);
  73.     BEGIN
  74.         s := Concat(s,s2);
  75.     END; (* SWriteString *)
  76.  
  77.